package com.mall.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.constant.MessageStatusEnum;
import com.mall.system.entity.EmailLog;
import com.mall.system.mapper.EmailLogMapper;
import com.mall.system.service.EmailLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 邮件发送日志服务实现类
 */
@Slf4j
@Service
public class EmailLogServiceImpl extends ServiceImpl<EmailLogMapper, EmailLog> implements EmailLogService {

    @Override
    public Long saveEmailLog(String toEmail, String subject, String content, Integer status, String errorMessage) {
        try {
            EmailLog emailLog = new EmailLog();
            emailLog.setToEmail(toEmail);
            emailLog.setSubject(subject);
            emailLog.setContent(content);
            emailLog.setStatus(status);
            emailLog.setErrorMessage(errorMessage);
            emailLog.setCreateTime(LocalDateTime.now());
            save(emailLog);
            return emailLog.getLogId();
        } catch (Exception e) {
            log.error("保存邮件发送日志失败: {}", e.getMessage());
            return null;
        }
    }
    
    @Override
    public boolean updateEmailLogStatus(Long logId, Integer status, String errorMessage) {
        try {
            EmailLog emailLog = getById(logId);
            if (emailLog != null) {
                emailLog.setStatus(status);
                emailLog.setErrorMessage(errorMessage);
                return updateById(emailLog);
            }
            return false;
        } catch (Exception e) {
            log.error("更新邮件发送状态失败: {}", e.getMessage());
            return false;
        }
    }
}